<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

class MailContentManagementPage {

	private $alerter;
	
	public function __construct() {
		$this->alerter = new Alerter();
	}

	/**
	 * adds extra access checks, dependent on page state
	 */
	function page_inc_check_access($page) {
		check_access("ADMIN");
	}

	/**
	 * execute page logic
	 */
	function page_inc_execute($page) {
		$CONTENT = array();

		if ($_POST['mail_id'] and !$_POST['submitted']) {
			$sql = db_query_select(array(array("*")), array("MAILCONTENTS"), "mail_id = {$_POST['mail_id']}");
			$mail = db_fetch_array(db_do_query($page, $sql));
			$CONTENT['title'] = $mail['description'];
			$CONTENT['body'] = "<p>Op deze pagina kan u de tekst van een bepaalde mail wijzigen. Daarbij kan u gebruik maken van volgende variabelen, die vervangen zullen worden bij het verzenden van de eigenlijke mail: {$mail['variables']}";
			$inputs = array();
			$creator = new FormCreator();
			$inputs[] = $creator->input_hidden("mail_id", $_POST['mail_id']);
			$inputs[] = $creator->input_text("Inhoud mail", "mail", db_desqlize($mail['mail']));
			$CONTENT['form'] = $creator->create("mailform", $inputs);
		} else {
			if ($_POST['submitted'] and $_POST['mail_id'] and $_POST['mail']) {
				$sql = db_query_update(array("mail"), array(db_sqlize($_POST['mail'])), "MAILCONTENTS", "mail_id = {$_POST['mail_id']}");
				db_do_query($page, $sql);
				$this->alerter->add_alert("mailing #{$_POST['mail_id']} aangepast");
				add_log("gebruiker #{$_SESSION['user_id']} paste mailing #{$_POST['mail_id']} aan");
			}
			$kv = get_keys_and_values("mail_id", "description", "MAILCONTENTS");
			$CONTENT['title'] = "Pas een mailing aan";
			$CONTENT['form'] = $creator->create("mailform", array($creator->input_select("Kies een mailing", "mail_id", $kv['keys'], $kv['values'], "maak een keuze", "", "document.mailform.submit()", 1, 0, 1)));
		}

		$page->content = $CONTENT;
	}

	/**
	 * dress up page content using skin stuff
	 */
	function page_inc_dress($page) {
		print_body($page);
		print_form($page, $page->content['form']);
	}
}

?>